package ut.servlet.member;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.ServletException;
public class CreateFreeTask extends HttpServlet{
	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
		request.setCharacterEncoding("utf-8");
		// 取得並驗證使用者輸入資訊
		String content;
		int from, to;
		if(request.getParameter("content") == null || request.getParameter("from_lan") == null || request.getParameter("to_lan") == null){
			return;
		}else{
			content = request.getParameter("content");
			if(content.length() > 200){
				return;
			}
			content = content.replace("&", "&amp;").replace("\"", "&quot;").replace("<", "&lt;").replace(">", "&gt;");
			content = content.replace("\r\n", "<br/>").replace("\n", "<br/>").replace("\r", "<br/>");
			try{
				from = Integer.parseInt(request.getParameter("from_lan"));
				to = Integer.parseInt(request.getParameter("to_lan"));
				if(ut.res.Language.getLanguage(from) == null || ut.res.Language.getLanguage(to) == null || from == to){
					return;
				}
			}catch(NumberFormatException e){
				return;
			}
		}
		// 確認使用者已經完成信箱驗證
		ut.data.User user = (ut.data.User)request.getSession().getAttribute("user");
		if(user.verify == 0){
			return;
		}
		// 將新的免費案件資料加入資料庫
		response.setHeader("result", ut.data.FreeTask.createFreeTask(user.id, from, to, content) + ""); // true: 成功、false: 失敗
	}
}